package fr.mav.gamecardcounter.Activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.util.ArrayList;

import fr.mav.gamecardcounter.Game;
import fr.mav.gamecardcounter.Player;
import fr.mav.gamecardcounter.R;
import fr.mav.gamecardcounter.TypeOfGame;

/**
 * Created by Quentin on 28/07/13.
 */
public class TarotGameActivity extends Activity {
    private Game game;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tarot_game);
        game = setupGame();
        Toast.makeText(this, game.getTypeOfGame().toString(), Toast.LENGTH_LONG).show();
    }

    private Game setupGame() {
        Game game = new Game();
        game.setTypeOfGame(TypeOfGame.Tarot);

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        LayoutInflater inflater = getLayoutInflater();
        builder.setTitle(R.string.legend_alert_dialog_game_activity);
        final View view = inflater.inflate(R.layout.alert_dialog_game_activity, null);
        builder.setView(view)
                .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                    }
                })
                .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        finish();
                    }
                });
        final AlertDialog alertDialog = builder.create();
        alertDialog.show();
        Button cancelButton = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int nbPlayer = 0;
                EditText editText = (EditText) view.findViewById(R.id.number_of_player_edit_text);
                try {
                    nbPlayer = Integer.parseInt(editText.getText().toString());
                } catch (Exception e) {
                    Toast.makeText(getApplicationContext(), getString(R.string.error_nb_player) + " " + editText.getText().toString(), Toast.LENGTH_SHORT).show();
                }
                if (nbPlayer > 1 && nbPlayer<=5) {
                    Toast.makeText(getApplicationContext(), "nb player: " + nbPlayer, Toast.LENGTH_SHORT).show();
                    initializeGame(nbPlayer);
                    alertDialog.dismiss();
                }else{
                    String message=getString(R.string.error_nb_player_inferior_0) +" "+nbPlayer ;
                    Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
                }
            }
        });
        return game;
    }

    private void initializeGame(int nbPlayer) {
        game.setNbPlayer(nbPlayer);

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        LayoutInflater inflater = getLayoutInflater();
        builder.setTitle(R.string.legend_alert_dialog_name_game_activity);
        final View view = inflater.inflate(R.layout.alert_dialog_name, null);
        builder.setView(view)
                .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                    }
                });
        final AlertDialog alertDialog = builder.create();
        alertDialog.show();
        for(int i =5;i>nbPlayer;i--){
            int idResource = getResources().getIdentifier("player_name_"+i, "id", getPackageName());
            EditText editText=(EditText) view.findViewById(idResource);
            editText.setVisibility(View.GONE);
        }
        Button cancelButton = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(verifyName(view)){
                    startGame();
                    alertDialog.dismiss();
                }else{
                    Toast.makeText(getApplicationContext(), R.string.error_invalid_name, Toast.LENGTH_SHORT).show();
                }
            }
        });
    }



    private boolean verifyName(View v) {
        boolean isValid=true;
        ArrayList<Player> players= new ArrayList<Player>();
        for(int i=1;i<=game.getNbPlayer();i++){
            EditText editText= (EditText) v.findViewById(getResources().getIdentifier("player_name_"+i, "id", getPackageName()));
            String name=editText.getText().toString();
            if (name!=null && !name.isEmpty()){
                Player player=new Player(name,0);
                players.add(player);
            }
            else{
                return false;
            }
        }
        game.setPlayersr(players);
        return isValid;
    }

    private void startGame() {
        LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear_layout_player_name);
        int s=getResources().getIdentifier("tarot_player_name_layout_"+game.getNbPlayer(), "layout", getPackageName());
        LayoutInflater inflater=(LayoutInflater)this.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        View view = inflater.inflate(s,linearLayout, false );
        linearLayout.addView(view);
        LinearLayout linearLayout1 = (LinearLayout) findViewById(R.id.linear_layout_player_score);
        s=getResources().getIdentifier("tarot_player_score_layout_"+game.getNbPlayer(), "layout", getPackageName());
        View view1 = getLayoutInflater().inflate(s, linearLayout1,false);
        linearLayout1.addView(view1);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(0, 0);
    }
}
